#include "test3.h"

// -----------------------------------------------------------------------------
void
tenacitas::balance_line::test3::
run( ) {

    f0( );
    std::cout << "sleeping for 10 secs..." << std::endl;    
    boost::this_thread::sleep(boost::posix_time::seconds(10));
    std::cout << "woke up" << std::endl;
    f1( );
    
}


// -----------------------------------------------------------------------------
void
tenacitas::balance_line::test3::
f1( ) {


    boost::filesystem::directory_iterator l_dir_begin( DIR_LOG );
    
    boost::filesystem::directory_iterator l_dir_end;

    for( boost::filesystem::directory_iterator l_dir_ite = l_dir_begin;
         l_dir_ite != l_dir_end;
         ++l_dir_ite ) {


        std::string l_filename = l_dir_ite->path( ).filename( ).string( );

        const boost::filesystem::directory_entry & l_dir_entry = *l_dir_ite;

        std::cout << l_filename << " has ";
        switch ( l_dir_entry.status( ).type( ) ) {
        case boost::filesystem::status_error:
            std::cout << "'status_error'";
            break;
        case boost::filesystem::file_not_found:
            std::cout << "'file_not_found'";
            break;

        case boost::filesystem::regular_file:
            std::cout << "'regular_file'";
        break;

        case boost::filesystem::directory_file:
            std::cout << "'directory_file'";
        break;

        case boost::filesystem::symlink_file:
            std::cout << "'symlink_file'";
        break;

        case boost::filesystem::block_file:
            std::cout << "'block_file'";
        break;

        case boost::filesystem::character_file:
            std::cout << "'character_file'";
        break;

        case boost::filesystem::fifo_file:
            std::cout << "'fifo_file'";
        break;

        case boost::filesystem::socket_file:
            std::cout << "'socket_file'";
        break;

        case boost::filesystem::type_unknown:
            std::cout << "'type_unknown'";
        break;
        default:
            std::cout << "UNRECOGNIZABLE";
            break;            
        }
        std::cout << " status" << std::endl;
        

        std::cout << "possible log file: '" << l_filename << "'... " ;

        std::string::size_type l_end_pos = l_filename.find( ".END" );        
        std::string l_filename_wo_end;
        
        
        if ( l_end_pos != std::string::npos ) {

            // we accept only if the original file, i.e., without the .END really
            // does not exist anymore
            l_filename_wo_end = std::string( DIR_LOG ) +
                std::string( "/") +
                l_filename.substr( 0, l_end_pos ); 

            if ( ( boost::filesystem::exists( l_filename_wo_end ) ) &&
                 ( boost::filesystem::is_regular_file( l_filename_wo_end ) ) ) {

                std::cout << "rejected, because " << l_filename_wo_end
                          << " exists!" << std::endl;
                
                std::cout << "sleeping for 1 secs..." << std::endl;    
                boost::this_thread::sleep(boost::posix_time::seconds(1));
            }
            
            else {

                // it is a log file
                std::cout << "accepted" <<  std::endl;
                // l_merger->add( log_file::ptr( new log_file( std::string( DIR_LOG ) +
                //                                             std::string( "/") +
                //                                             l_filename ) ) );
            }
        }
        else {
            std::cout << "rejected, because it does not end in '.END'" <<
                std::endl; 
        }
    }

}

// -----------------------------------------------------------------------------
void
tenacitas::balance_line::test3::
f2( std::int16_t p_repeats,
    std::int16_t p_stop_at,
    std::int32_t p_sleep,
    const std::string & p_prefix ) {

    const std::string l_filename( std::string( DIR_LOG ) +
                                  std::string ( "/" ) +
                                  p_prefix + 
                                  std::string( ".log" ) );

    {
        
        std::ofstream l_file( l_filename.c_str( ) );

        std::int16_t l_counter = 0;

        l_file << p_prefix << ", "
               << p_repeats << ", "
               << p_stop_at << ", "
               << p_sleep;
    
        for ( std::int16_t l_i = 0; l_i < p_repeats; ++l_i ) {
            if ( l_counter >= p_stop_at ) {
                l_file << p_prefix << " counter = " << l_counter << ", so gone sleep";
            
                boost::this_thread::sleep(boost::posix_time::milliseconds(p_sleep));
                l_counter = 0;
                l_file << p_prefix << " woke up";
            }

            ++l_counter;

            l_file << p_prefix << ", " << l_i << " executions";        
        }



        std::cout << p_prefix << " renaming"  << std::endl;
        const std::string l_new_filename( l_filename + std::string( ".END" ) );
        boost::filesystem::rename( l_filename, l_new_filename );

    }

    std::cout << p_prefix << " leaving"  << std::endl;
}

// -----------------------------------------------------------------------------
void
tenacitas::balance_line::test3::
f0( ) {

    // boost::thread l_01( f2, 100, 20, 200, "one");

    // boost::thread l_02( f2, 300, 250, 500, "two");

    // boost::thread l_03( f2, 1000, 800, 300, "three");

    // boost::thread l_04( f2, 200, 30, 900, "four");

    // boost::thread l_05( f2, 5000, 1000, 1000, "five"); 

    // boost::thread l_06( f2, 100, 1, 200, "six");

    // boost::thread l_07( f2, 700, 130, 300, "seven");

    // boost::thread l_08( f2, 2000, 1000, 100, "eight");

    // boost::thread l_09( f2, 900, 50, 1000, "nine");

    // boost::thread l_10( f2, 10, 2, 500, "ten");

    boost::thread l_11( f2, 5, 2, 200, "eleven");

    // l_01.join( );
    // l_02.join( );
    // l_03.join( );
    // l_04.join( );
    // l_05.join( );
    // l_06.join( );
    // l_07.join( );
    // l_08.join( );
    // l_09.join( );
    // l_10.join( );
    l_11.join( );

//    boost::this_thread::sleep(boost::posix_time::seconds(2));
}
